/**
 * 物料管理数据model
 */
import {getPageManageList,saveObj,deleteObj,getList,getInfo} from 'api/teamManageApi';

const teamManage = {
    state: {
        teamManagePageData:[],
        teamSelectList:[],
        page: {
            currentPage: 1,
            pageSize: 5,
            total: 0
        },
        queryPara:{
            name: '',
        }
    },
    mutations: {
        loadTeamManageMut(state, obj){
            if (obj.rows !== '') {
                state.teamManagePageData = obj.rows;
                state.page.currentPage = obj.currentPage;
                state.page.pageSize = obj.pageSize;
                state.page.total = obj.total;
            }
        },
        loadTeamSelectMut(state, obj){
            let teamSelectList = [];
            if (obj.code === 0) {
                if (obj.data && obj.data.length > 0){
                    obj.data.forEach((item, index) => {
                        teamSelectList.push({
                            index : index,
                            value : item.recordId,
                            label : item.name  ,
                            record:{...item}
                        })
                    })
                }
                state.teamSelectList = teamSelectList;
            }
        },
        setTeamManageQueryPrams(state, obj){
            state.queryPara = obj;
        }

    },
    actions: {
        /*获取分页列表*/
        getTeamManagePageListAct (context, para) {
            return getPageManageList(para).then(res => {
                context.commit("loadTeamManageMut",res.data);
            });
        },
        /*获取列表*/
        getTeamSelectListAct (context, para) {
            return getList(para).then(res => {
                context.commit("loadTeamSelectMut",res.data);
            });
        },
        /*新增/编辑*/
        saveTeamAct (context, para) {
            return saveObj(para).then(res=>{
                return res.data;
            });
        },
        /*详情*/
        getTeamInfoAct (context, para) {
            return getInfo(para).then(res=>{
                return res.data;
            });
        },
        /*删除*/
        deleteTeamAct (context, para) {
            return deleteObj(para).then(res=>{
                return res.data;
            });
        },

    }
};

export default teamManage;
